/**
 * Copyright (2017, ) Institute of Software, Chinese Academy of Sciences
 */
package cn.abcsys.cloud.devops.runtime.incubates.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author wuheng(wuheng@otcaix.iscas.ac.cn)
 * @date   Jul 10, 2017
 *
 */
public class UrlUtils {

	private static Pattern pathPattern = Pattern.compile("[a-z0-9A-Z-._/]*");
	
	/**
	 * IP地址
	 */
	private static Pattern ipPattern = Pattern.compile("\\b((?!\\d\\d\\d)\\d+"
			+ "|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d"
			+ "|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\b");
	
	public static boolean validIP(String ip) {
		Matcher matcher = ipPattern.matcher(ip);
		return matcher.matches();
	} 
	
	/**
	 * 判断路径的有效性
	 * 
	 * @param path
	 * @return
	 */
	public static boolean validPath(String path) {
		Matcher matcher = pathPattern.matcher(path);
		return matcher.matches();
	}
	
	public static boolean validPort(int port) {
		return (port > 0 && port < Integer.MAX_VALUE) ? true : false;
	}
	
	public static void main(String[] args) {
		
		System.out.println(UrlUtils.validIP("192"));
		System.out.println(UrlUtils.validIP("192.168"));
		System.out.println(UrlUtils.validIP("192.168.1"));
		System.out.println(UrlUtils.validIP("192.168.1.1.1"));
		
		System.out.println(UrlUtils.validIP("-1.192.168.1"));
		System.out.println(UrlUtils.validIP("256.192.168.1"));
		System.out.println(UrlUtils.validIP("0.192.168.1"));
		System.out.println(UrlUtils.validIP("255.192.168.1"));
		
		System.out.println(UrlUtils.validPath(""));
		System.out.println(UrlUtils.validPath("/home/abc"));
	}
}
